﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ini编辑器CS
{
    public class Card
    {
        public string ID;
        public string Name;
        public string Type;
        public DynamicImg[] img;
        CardPile parent;
        public string path;
        const int Normal = 0;
        const int Flip = 1;
        public Card(CardPile _parent)
        {
            ID = "";
            Name = "新卡牌";
            Type = "Card";
            parent = _parent;
            img = new DynamicImg[2];
            img[Normal] = new DynamicImg();
            img[Flip] = new DynamicImg();
        }
        public Card(CardPile _parent, string _path,string _ID)
        {
            parent = _parent;
            path = _path;
            ID = _ID;
            Load();
            display();
            
            Console.WriteLine("卡牌"+Name+"加载完成");
        }
        public void Load()
        {
            Loadini();
        }
        public void Loadini()
        {
            StreamReader sr = new StreamReader(path + "config.ini", Encoding.UTF8);
            while (sr.Peek() != -1)
            {
                if (Convert.ToChar(sr.Peek()) == '[')
                    LoadSection(sr);
                else
                    sr.ReadLine();
            }
            sr.Close();
        }
        void LoadSection(StreamReader sr)
        {
            string str = sr.ReadLine();
            if (str == "[Info]")
                LoadInfo(sr);
            else if (str == "[Image]")
                LoadImage(sr);
        }
        void LoadInfo(StreamReader sr)
        {
            string s;
            while (sr.Peek() != -1)
            {
                if (Convert.ToChar(sr.Peek()) == '[')
                {
                    LoadSection(sr);
                    break;
                }
                else
                {
                    s = sr.ReadLine();
                    string[] sArray = s.Split('=');
                    if (sArray[0] == "Name")
                        Name = sArray[1];
                    else if (sArray[0] == "Type")
                        Type = sArray[1];
                }
            }
        }
        void LoadImage(StreamReader sr)
        {
            img = new DynamicImg[2];
            string s;
            while (sr.Peek() != -1)
            {
                if (Convert.ToChar(sr.Peek()) == '[')
                {
                    LoadSection(sr);
                    break;
                }
                else
                {
                    s = sr.ReadLine();
                    string[] sArray = s.Split('=');
                    if (sArray[0] == "Normal")
                        img[Normal] = new DynamicImg(path + sArray[1]);
                    else if (sArray[0] == "Flip")
                    img[Flip] = new DynamicImg(path + sArray[1]);
                }
            }
        }
        void display()
        {
            Console.WriteLine(Name);
            Console.WriteLine(Type);
            Console.WriteLine(Normal);
            Console.WriteLine(Flip);
        }
        public void Save()
        {
            if (Directory.Exists(path) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(path);
            }
            StreamWriter sw = new StreamWriter(path + "\\config.ini",false);
            sw.WriteLine("[Info]");
            sw.WriteLine("Name=" + Name);
            sw.WriteLine("Type=" + Type);
            sw.WriteLine("[Image]");
            sw.WriteLine("Normal=image\\" + Path.GetFileName(img[Normal].path));
            sw.WriteLine("Flip=image\\" + Path.GetFileName(img[Flip].path));
            sw.Close();//写入
            Directory.CreateDirectory(path + "image");
            if (img[Normal].path != path + "image\\" + Path.GetFileName(img[Normal].path))
                File.Copy(img[Normal].path, path + "image\\" + Path.GetFileName(img[Normal].path), true);
            if (img[Flip].path != path + "image\\" + Path.GetFileName(img[Flip].path))
            File.Copy(img[Flip].path, path + "image\\" + Path.GetFileName(img[Flip].path), true);
        }
    }
}
